package com.tuozixuan.spring6.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tuozixuan.spring6.dao.BookDao;
import com.tuozixuan.spring6.service.BookService;

@Service
public class BookServiceImpl implements BookService {

	@Autowired
	private BookDao bookDao;

	@Override
	@Transactional
//	@Transactional(timeout = 3)
//	@Transactional(noRollbackFor = ArithmeticException.class)
//	@Transactional(noRollbackForClassName = "java.lang.ArithmeticException")
//	@Transactional(isolation = Isolation.DEFAULT)//使用数据库默认的隔离级别
//	@Transactional(isolation = Isolation.READ_UNCOMMITTED)//读未提交
//	@Transactional(isolation = Isolation.READ_COMMITTED)//读已提交
//	@Transactional(isolation = Isolation.REPEATABLE_READ)//可重复读
//	@Transactional(isolation = Isolation.SERIALIZABLE)//串行化
	public void buyBook(Integer bookId, Integer userId) {
		
		// 查询图书的价格
		Integer price = bookDao.getPriceByBookId(bookId);
		
		// 更新图书的库存
		bookDao.updateStock(bookId);
		
		// 更新用户的余额
		bookDao.updateBalance(userId, price);
	}

}
